package com.ruoyi.eqmonitor.controller;

import java.util.List;

import com.ruoyi.eqmonitor.domain.OptDeviceRelation;
import com.ruoyi.eqmonitor.service.IOptDeviceRelationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备与其他事物的关联关系
 * @author ruoyi
 * @date 2022-12-16
 */
@RestController
@RequestMapping("/relation")
public class OptDeviceRelationController extends BaseController{

    @Autowired
    private IOptDeviceRelationService optDeviceRelationService;

    /**
     * 设备与其他事物的关联关系列表
     */
    @ApiOperation("设备与其他事物的关联关系列表")
    @GetMapping("/list")
    public TableDataInfo<?> list(OptDeviceRelation optDeviceRelation){
        startPage();
        List<OptDeviceRelation> list = optDeviceRelationService.selectOptDeviceRelationList(optDeviceRelation);
        return getDataTable(list);
    }

    /**
     * 根据设备编码查询设备与其他事物的关联关系明细
     */
    @ApiOperation("根据设备编码查询设备与其他事物的关联关系明细")
    @GetMapping(value = "/{deviceCode}")
    public AjaxResult getInfo(@PathVariable("deviceCode") String deviceCode){
        return AjaxResult.success(optDeviceRelationService.selectOptDeviceRelationByDeviceCode(deviceCode));
    }

    /**
     * 新增设备与其他事物的关联关系
     */
    @ApiOperation("新增设备与其他事物的关联关系")
    @Log(title = "设备与其他事物的关联关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptDeviceRelation optDeviceRelation){
//    	optDeviceRelation.setCreateTime(new Date());
//    	optDeviceRelation.setCreateBy(SecurityUtils.getUsername());
        return toAjax(optDeviceRelationService.save(optDeviceRelation));
    }

    /**
     * 修改设备与其他事物的关联关系
     */
    @ApiOperation("修改设备与其他事物的关联关系")
    @PutMapping
    public AjaxResult edit(@RequestBody OptDeviceRelation optDeviceRelation){
//    	optDeviceRelation.setUpdateTime(new Date());
//    	optDeviceRelation.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optDeviceRelationService.updateById(optDeviceRelation));
    }

    /**
     * 删除设备与其他事物的关联关系
     */
    @ApiOperation("删除设备与其他事物的关联关系")
    @Log(title = "设备与其他事物的关联关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceCodes}")
    public AjaxResult remove(@PathVariable String[] deviceCodes){
        return toAjax(optDeviceRelationService.removeByIds(Arrays.asList(deviceCodes)));
    }

    /**
     * 导出设备与其他事物的关联关系列表
     */
    @ApiOperation("导出设备与其他事物的关联关系列表")
    @Log(title = "设备与其他事物的关联关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptDeviceRelation optDeviceRelation){
        List<OptDeviceRelation> list = optDeviceRelationService.selectOptDeviceRelationList(optDeviceRelation);
        ExcelUtil<OptDeviceRelation> util = new ExcelUtil<OptDeviceRelation>(OptDeviceRelation.class);
        util.exportExcel(response, list, "设备与其他事物的关联关系数据");
    }

    /**
     * 根据关联关系分组查询每个片区
     */
    @ApiOperation("根据关联关系分组查询每个片区")
    @GetMapping(value = "/reAreaList")
    public AjaxResult getAreaList(){
        return AjaxResult.success(optDeviceRelationService.selectOptReArea());
    }

}
